var YYALogin = function() {	
	this.bodyHeight = $(document).height();
	this.bodyWidth = screen.width;
	this.id = '';
	
	this.init = function( elm )
	{
		this.opacityElm();
		
		if( $('body #YYALoginFrm_'+this.id).size()== 0){	
			var elm_id = 'YYALoginFrm_'+this.id;
			$('body').append('<div class="YYALoginFrm" id="'+elm_id+'"></div>');
			$('#YYALoginFrm_'+this.id).append('<div class="YYALoginTitle"></div>');
			$('#YYALoginFrm_'+this.id).append('<div class="YYALoginContentBor"></div>');
			$('#'+elm_id+' .YYALoginContentBor').empty();
			$('#'+elm_id+' .YYALoginContentBor').append('<div class="YYALoginContent"></div>');
			$('#'+elm_id+' .YYALoginContent').empty();
			$('#'+elm_id+' .YYALoginContentBor').append('<div class="YYALoginClose"></div>');
			$('#'+elm_id+' .YYALoginClose').append('<span class="uiBtn confirm">&#272;&#243;ng</span>');
		}			
	};
	
	this.opacityElm = function(){
		if( $('body .YYALogin_opacity').size()== 0){			
			$('body').append('<div class="YYALogin_opacity"></div>');
		}
		
		$('.YYALogin_opacity').css({
			height: this.bodyHeight
		});
		$('.YYALogin_opacity').show();
	};

	this.dialog = function( elm, title ){	
		this.id = elm.substr(1);
		this.init( elm );
		var this_ = this;
		
		if( $(elm).parent().attr('class') != 'YYALoginContent' ){		
			$(elm +' iframe').attr('src', $(elm +' iframe').attr('data') );
			$(elm +' iframe').attr('data','');
			$(elm).appendTo('#YYALoginFrm_'+this_.id+' .YYALoginContent');
		}
		
		$('#YYALoginFrm_'+this_.id+' .YYALoginTitle').html( title );
		
		$('#YYALoginFrm_'+this_.id).show();
		
		$(elm).show();
		
		var dialogWidth = $('#YYALoginFrm_'+this_.id ).outerWidth();
		var scrollTop = ( $(window).height() - $('#YYALoginFrm_'+this_.id ).outerHeight() )/2
		
		$('#YYALoginFrm_'+this_.id ).css({
			left: (this_.bodyWidth-dialogWidth)/2,
			top: (scrollTop <= 0 ) ? 0 : scrollTop+$(window).scrollTop()
		});
		
		$('.YYALoginClose .uiBtn').bind('click', function(){
			this_.close( this );
			return false;
		});
		
	};
	
	this.close = function( btn ){
		$('.YYALogin_opacity').hide();
		$(btn).parents('.YYALoginFrm').hide();
	};
	
	this.keyClose = function(){
		var dialogShow = $('body .YYALoginFrm');
		if( dialogShow.is(':visible') ){
			dialogShow.hide();
			$('.YYALogin_opacity').hide();
		}
	};
};

function LoginDialog(){
	var YYALogin_ = new YYALogin();
	YYALogin_.dialog('#DialogLogin','&#272;&#259;ng nh&#7853;p h&#7879; th&#7889;ng');
}

function RegisterDialog(){
	var YYALogin_ = new YYALogin();
	YYALogin_.dialog('#DialogRegister','&#272;&#259;ng k&#253; th&#224;nh vi&#234;n');
}

$(function(){	
	$('head').append('<link href="https://kayota.googlecode.com/svn/login/btn.css" rel="stylesheet" media="all" type="text/css" />');
	$('head').append('<link href="https://kayota.googlecode.com/svn/login/style.css" rel="stylesheet" media="all" type="text/css" />');
	
	var YYALogin_ = new YYALogin();
	
	$('#DialogLoginLnk').click(function(){
		YYALogin_.dialog('#DialogLogin','&#272;&#259;ng nh&#7853;p h&#7879; th&#7889;ng');
		//YYALogin_.dialog('#DialogLogin','Đăng nhập hệ thống');
	});
	
	$('#DialogRegisterLnk').click(function(){
		YYALogin_.dialog('#DialogRegister','&#272;&#259;ng k&#253; th&#224;nh vi&#234;n');
		//YYALogin_.dialog('#DialogRegister','Đăng ký thành viên');
	});
	
});
	
$(document).bind('keypress keydown keyup',function(e){
	if ( e.keyCode == 27 ){
		var dialogShow = $('body .YYALoginFrm');
		if( dialogShow.is(':visible') ){
			var YYALogin_ = new YYALogin();
			e.preventDefault();
			YYALogin_.keyClose();
			return false;
		}
	}
});